/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.unice.miage.oobdoo.entity;

import com.unice.miage.oobdoo.helpfull.LocalEncrypter;
import java.io.Serializable;
import java.security.Key;
import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Jeremy Romano
 */
@Entity
@XmlRootElement
public class Utilisateur implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String login;
    private byte[] password;
    private String email;
    private String uniquekey;
    private String access_token_fb;
    private String access_token_picasa;
    private String access_token_flickr;
    
    private long lastLogin;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private Collection<Album> albums;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private Collection<Commentaire> commentaires;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private Collection<Note> notes;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private Collection<Partage> partages;

    public Utilisateur() {
    }

    public Utilisateur(String login, String password, String email) {
        try {
            this.login = login;
            this.password = LocalEncrypter.encrypt(password);
            this.email = email;
            this.uniquekey = UUID.randomUUID().toString();
            this.albums = new ArrayList<Album>();
            this.commentaires = new ArrayList<Commentaire>();
            this.notes = new ArrayList<Note>();
            this.partages = new ArrayList<Partage>();
            this.access_token_fb = null;
            this.access_token_picasa = null;
        } catch (Exception ex) {
            Logger.getLogger(Utilisateur.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Collection<Album> getAlbums() {
        return albums;
    }

    public Collection<Partage> getPartages() {
        return partages;
    }

    public void addPartage(Partage p) {
        this.partages.add(p);
    }

    public void setPartages(Collection<Partage> partages) {
        this.partages = partages;
    }

    public Collection<Album> getAlbumsPartagesValide() {
        ArrayList<Album> albumsPartages = new ArrayList<Album>();
        for (Partage p : this.partages) {
            if (p.isValide()) {
                albumsPartages.add(p.getAlbum());
            }
        }
        return albumsPartages;
    }

    public void addAlbum(Album a) {
        this.albums.add(a);
    }

    public Collection<Note> getNotes() {
        return notes;
    }

    public void addNote(Note n) {
        this.notes.add(n);
    }

    public void addCommentaire(Commentaire c) {
        this.commentaires.add(c);
    }

    public Collection<Commentaire> getCommentaires() {
        return commentaires;
    }

    public boolean isGoodPassword(String password) {
        try {
            if ((LocalEncrypter.decrypt(this.password)).equals(password)) {
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            Logger.getLogger(Utilisateur.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public long getLastLogin() {
        return lastLogin;
    }

    public void setLastLogin(long lastLogin) {
        this.lastLogin = lastLogin;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }
    
    public String getUniquekey() {
        return uniquekey;
    }

    public void setPassword(String password) {
        try {
            this.password = LocalEncrypter.encrypt(password);
        } catch (Exception ex) {
            Logger.getLogger(Utilisateur.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public String toString() {
        return "Utilisateur{"
                + "id="
                + id
                + "; login="
                + login
                + "; password="
                + password
                + "; email="
                + email
                + "; lastLogin="
                + lastLogin
                + '}';
    }

    public String getAccess_token_fb() {
        return access_token_fb;
    }

    public void setAccess_token_fb(String access_token_fb) {
        this.access_token_fb = access_token_fb;
    }

    public String getAccess_token_picasa() {
        return access_token_picasa;
    }

    public void setAccess_token_picasa(String access_token_picasa) {
        this.access_token_picasa = access_token_picasa;
    }

    public String getAccess_token_flickr() {
        return access_token_flickr;
    }

    public void setAccess_token_flickr(String access_token_flickr) {
        this.access_token_flickr = access_token_flickr;
    }
}
